﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace GestaltDesigner
{
    // Custom class implements the IValueConverter interface.
    public class ImageSourceToStringConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                BitmapImage image = (BitmapImage)value;
                if (!string.IsNullOrEmpty(image.UriSource.OriginalString))
                    return image.UriSource.OriginalString;
                else
                    return "";

            }
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Uri uri;
            if (value != null)
            {
                try
                {
                    uri = new Uri(value.ToString(),UriKind.RelativeOrAbsolute);
                    return new BitmapImage(uri);
                }
                catch
                {
                    return null;
                }
            }
            else
                return value;
                 
        }

        #endregion
    }
    public class UriToStringConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                Uri uri = (Uri)value;
                if (!string.IsNullOrEmpty(uri.OriginalString))
                    return uri.OriginalString;
                else
                    return "";

            }
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Uri uri;
            if (value != null)
            {
                try
                {
                    uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
                    return uri;
                }
                catch
                {
                    return new Uri("...", UriKind.RelativeOrAbsolute); 
                }
            }
            else
                return value;

        }

        #endregion
    }

    public class DeepZoomImageTileSourceToStringConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                DeepZoomImageTileSource image = (DeepZoomImageTileSource)value;
                if (!string.IsNullOrEmpty(image.UriSource.OriginalString))
                    return image.UriSource.OriginalString;
                else
                    return "";

            }
            else
                return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Uri uri;
            if (value != null)
            {
                try
                {
                    uri = new Uri(value.ToString(),UriKind.RelativeOrAbsolute);
                    return new DeepZoomImageTileSource(uri);
                }
                catch
                {
                    return null;
                }
            }
            else
                return value;

        }

        #endregion
    }

}
